package com.chenwenxuan.sync.console.mail;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;

public class MailClient {
	
	private String host;
	private String user;
	private String password;
	private String from;
	private boolean auth;
	private boolean debug;
	private boolean ssl;
	private int port;
	
	public MailClient() {
	}

	public void sendTextMail(String[] to, String title, String text) throws AddressException,
			MessagingException, EmailException {
		Email email = creatTxtMail();
		email.setSubject(title);
		email.setMsg(text);
		for (String t : to) {
			email.addTo(t);
		}
		email.send();
	}
	
	public void sendPicMail(String[] to, String title, String text, File file) throws AddressException, MessagingException, EmailException {
		
		HtmlEmail email = createHtmlMail();

		for (String user : to)
			email.addTo(user);

		email.setSubject(title);
		String cid = email.embed(file, "image");
		email.setHtmlMsg("<html>" + text + "<br/><img src=\"cid:" + cid
				+ "\"></html>");
		email.send();

	}
	
	public void sendHtmlMail(String[] to, String title, String text)
			throws AddressException, MessagingException, EmailException {

		HtmlEmail email = createHtmlMail();
		for (String user : to)
			email.addTo(user);
		email.setFrom(from);
		email.setSubject(title);
		email.setHtmlMsg(text);
		email.send();

	}
	
	private HtmlEmail createHtmlMail() throws EmailException {
		
		HtmlEmail email = new HtmlEmail();
		email.setDebug(debug);
		email.setAuthenticator(new DefaultAuthenticator(user, password));
		email.setHostName(host);
		email.setSSLOnConnect(ssl);
		email.setSmtpPort(port);
		email.setFrom(from);
		
		return email;
	}
	
	private Email creatTxtMail() throws EmailException {
		Email email = new SimpleEmail();
		email.setDebug(debug);
		email.setAuthenticator(new DefaultAuthenticator(user, password));
		email.setHostName(host);
		email.setSSLOnConnect(ssl);
		email.setSmtpPort(port);
		email.setFrom(from);
		return email;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public boolean isAuth() {
		return auth;
	}

	public void setAuth(boolean auth) {
		this.auth = auth;
	}

	public boolean isDebug() {
		return debug;
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	public boolean isSsl() {
		return ssl;
	}

	public void setSsl(boolean ssl) {
		this.ssl = ssl;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}
	
	

}
